package com.sm.activities.subscriptionmanager;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.sm.R;
import com.sm.activities.WelcomeActivity;
import com.sm.bo.Course;
import com.sm.tools.MyCustomCourseAdapter;
import com.sm.tools.NewModifyCourseActivity;

public class CoursesManager_Tab extends Activity {

	private static final int REQUEST_NEW_COURSE_CODE = 10;
//	private ArrayAdapter arrayAdapter;
//	private ListView listview;
	MyCustomCourseAdapter dataAdapter = null;	
	Button bn;
	Button be;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_courses_manager);
		displayCoursesList();
		bn = (Button) findViewById(R.id.BtnNewC);
		be = (Button) findViewById(R.id.BtnEditC);
		
		bn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Button b = (Button) v;
				// Toast.makeText(getApplicationContext(),
				// "Clicked on : " + dataAdapter.getSelectedIndex(),
				// Toast.LENGTH_LONG)
				// .show();
		Intent newCourse = new Intent(getApplicationContext(),NewModifyCourseActivity.class);
				newCourse.putExtra("NewOrModify", "New");
				startActivityForResult(newCourse, REQUEST_NEW_COURSE_CODE);
			}
		});
		
		be.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Button b = (Button) v;
				// WelcomeActivity.coursesList.remove(b.getId() - 10);
		 Intent modifyCourse = new Intent(getApplicationContext(),NewModifyCourseActivity.class);
				modifyCourse.putExtra("NewOrModify", "Modify");
				modifyCourse.putExtra("course", new Course("Coucou", "Moi c Mak"));
				startActivityForResult(modifyCourse, REQUEST_NEW_COURSE_CODE);
			}
		});
	}

	private void displayCoursesList() {
		// create an ArrayAdaptar from the String Array
		dataAdapter = new MyCustomCourseAdapter(this, R.layout.courselist_view,
				WelcomeActivity.coursesList);
		ListView listView = (ListView) findViewById(R.id.listView1);
		// Assign adapter to ListView
		listView.setAdapter(dataAdapter);
		listView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// When clicked, show a toast with the TextView text
				Course course = (Course) parent.getItemAtPosition(position);
				Toast.makeText(getApplicationContext(),
						"Clicked on Row: " + course.getWording(),
						Toast.LENGTH_SHORT).show();
				dataAdapter.setSelectedIndex(position);
			}
		});

	}

	private void checkButtonClick() {

		// Button myButton = (Button) findViewById(R.id.findSelected);
		// myButton.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		//
		// StringBuffer responseText = new StringBuffer();
		// responseText.append("The following were selected...\n");
		//
		// ArrayList<Country> countryList = dataAdapter.countryList;
		// for(int i=0;i<countryList.size();i++){
		// Country country = countryList.get(i);
		// if(country.isSelected()){
		// responseText.append("\n" + country.getName());
		// }
		// }
		//
		// Toast.makeText(getApplicationContext(),
		// responseText, Toast.LENGTH_LONG).show();
		//
		// }
		// });

	}

}
